package com.billsharing.server;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class Group {
static int gid;
String groupName;
int groupCreator;
ArrayList<User> groupMembers = new ArrayList<User>();
boolean result;


Group(int grpid)
{
	DbConnect dbconn = new DbConnect();
	Connection conn;
	conn = dbconn.getConnection();
	try
	{
	PreparedStatement groupQuery;
	String sql = "select * from Groups where groupid = ?";
	groupQuery = conn.prepareStatement(sql);
	groupQuery.setInt(1, grpid);
	ResultSet rs = groupQuery.executeQuery();
	
	while(rs.next())
	{
		gid = rs.getInt("groupid");
		groupName = rs.getString("groupname");
		groupCreator = rs.getInt("creatorid");
	}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	
	
			}





Group(String grpName, int uid)
{
	try
	{
		DbConnect dbconn = new DbConnect();
		Connection conn;
		conn = dbconn.getConnection();
		Statement s = conn.createStatement();
		Statement s1 = conn.createStatement();
		System.out.println("Add grp"+grpName+"  "+uid);
		int i=s.executeUpdate("insert into Groups (groupname,creatorid) values ('"+grpName+"',"+uid+")");
		
		if(i>0)
		{
			result=true;
			PreparedStatement groupQuery;
			String sql = "select * from Groups where groupname = ?";
			groupQuery = conn.prepareStatement(sql);
			groupQuery.setString(1, grpName);
			ResultSet rs = groupQuery.executeQuery();
			
			while(rs.next())
			{
				gid = rs.getInt("groupid");
				groupName = rs.getString("groupname");
				groupCreator = rs.getInt("creatorid");
				int i1=s1.executeUpdate("insert into GroupMembers values("+gid+","+groupCreator+")");
			}
			
			
					}
		else
		{
			result=false;
		}
		
		
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	
}







public Group() {
	// TODO Auto-generated constructor stub
}





boolean addUser(User user)
{
	
	try
	{
		DbConnect dbconn = new DbConnect();
		Connection conn;
		conn = dbconn.getConnection();
		Statement s = conn.createStatement();
		PreparedStatement groupQuery;
		String sql = "insert into GroupMembers (groupid,groupuserid) values (?,?)";
		groupQuery = conn.prepareStatement(sql);
		groupQuery.setInt(1, gid);
		groupQuery.setInt(2,user.getUid());
		int i = groupQuery.executeUpdate();
		
	
		if(i>0)
		{
			result=true;
			System.out.println("Add grp"+gid+"  "+user.getUid());
			
					}
		else
		{
			result=false;
			System.out.println(result);
		}
		
		
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	return result;
	
}

void deleteUser(User user)
{
    
}

int getGid()
{
	return gid;
}

int getGid(String grpname)
{
	try
	{
		DbConnect dbconn = new DbConnect();
    	Connection conn = dbconn.getConnection();
    	Statement s = conn.createStatement();
    	s.executeQuery("select groupid from Groups where groupname='"+grpname+"'");
    	ResultSet rs=s.getResultSet();
    	while(rs.next())
    	{
    		gid=rs.getInt("groupid");
    	}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	return gid;
}

String getGroupName()
{
	return groupName;
}

ArrayList<User> getUsersList()
{
    return groupMembers;
}

}